<?php
/**
 * Joomla! 1.5 component Bbits Simple directory
 *
 * @version $Id: view.html.php 2011-07-08 03:53:16 svn $
 * @author Miguel Puig
 * @URL http://miguelpuig.com
 * @package Joomla
 * @subpackage Bbits Simple directory
 * @license GNU/GPL
 *
 * Simple Directory by bittingbits.com
 *
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');


jimport( 'joomla.application.component.controller' );
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'helper.php' );

/**
 * Bbits Simple directory Controller
 *
 * @package Joomla
 * @subpackage Bbits Simple directory
 */
class BbitsdControllerItems extends JController {
    /**
     * Constructor
     * @access private
     * @subpackage Bbits Simple directory
     */
   function __construct() {
    //Get View
    if(JRequest::getCmd('view') == '') {
        JRequest::setVar('view', 'default');
    }
    $this->item_Item = 'Default';
    parent::__construct();

    $this->registerTask('addItem', 'editItem');
    
    }
     
   function Items()
   {

        
        $view =& $this->getView('items', 'html');
        $view->setModel($this->getModel('items'), true);
		$view->setModel( $this->getModel( 'countries' ) );
		$view->setModel( $this->getModel( 'types' ) );
		
		$view->display();
        
   }
   
   function editItem()
    {
    	
        JRequest::setVar('view', 'editItem');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('items');
        
        $user =& JFactory::getUser();
        
        $view =& $this->getView('editItem', 'html');
        $view->setModel($this->getModel('items'), true);
		$view->setModel( $this->getModel( 'groups' ) );
		$view->setModel( $this->getModel( 'countries' ) );
		$view->setModel( $this->getModel( 'types' ) );
        
        $view->display();
        
    }
    function saveItem() {
        $model = $this->getModel('items');
        $user =& JFactory::getUser();
       
        
        // applying all changes
        $id = $model->store();

    
        $link 	= 'index.php?option=com_bbitsd&task=items';
        $this->setRedirect($link, JText::_('COM_BBSDIR_ITEM_SAVED'));
    }
   function applyitem()
    {
        JRequest::setVar('view', 'editItem');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('items');
		
        $user =& JFactory::getUser();

        $id = $model->store();

        JRequest::setVar('cid', array($id));

        $view =& $this->getView('editItem', 'html');
        $view->setModel($this->getModel('items'), true);
        $view->setModel( $this->getModel( 'groups' ) );
		$view->setModel( $this->getModel( 'countries' ) );
		$view->setModel( $this->getModel( 'types' ) );
        $view->display();
    } 
   function deleteItem()
    {
        $model = $this->getModel('items');
        $cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
        
        if (!is_array($cid) || count($cid) < 1) {
            $msg = '';
			JError::raiseWarning(500, JText::_( 'COM_BBSDIR_SELECT_DELETE' ) );
        } else {
            foreach ($cid as $id) {
                if (!$model->delete($id)) {
                    JError::raiseError(500, JText::_('COM_BBSDIR_DELETE_FAILED').'<pre>'.print_r($cid, true).'</pre>');
                }
            }
            
            $msg = count($cid).' '.JText::_( 'COM_BBSDIR_SELECTED_DELETE' );
			$cache = &JFactory::getCache('com_bbitsd');
			$cache->clean();
        }
        
        $this->setRedirect( 'index.php?option=com_bbitsd&task=items', $msg );
    }
    
	function saveorder()
	{
		global $option;
		// Initialize variables
		$db	=& JFactory::getDBO();
		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$total	= count( $cid );
		
		$order= JRequest::getVar( 'order', array(0), 'post', 'array' );
		JArrayHelper::toInteger($order, array(0));


		$row =& JTable::getInstance('items', 'Table');
		$groupings = array();
	 
		// update ordering values
		for( $i=0; $i < $total; $i++ ) {
			$row->load( (int) $cid[$i] );
	// echo "ACTUAL: ".$row->ordering." - NUEVO: ".$order[$i]."<br>";
			if ($row->ordering != $order[$i]) {
				$row->ordering = $order[$i];
				$groupings[] = $row->country;
				if (!$row->store()) {
					JError::raiseError(500,$db->getErrorMsg() );
				}
			}
		}
	 
	// execute updateOrder for each parent group
	$groupings = array_unique( $groupings );
	foreach ($groupings as $group){
		$row->reorder('catid = '.(int) $group);
	}
		
	//	$row->reorder();
	 
		$msg 	= 'New ordering saved in: '.$total;
		$this->setRedirect('index.php?option='.$option.'&task=items', $msg);
	}
	




	function orderUp(){
		global $mainframe;

		// Initialize variables
		$db =& JFactory::getDBO();
	
		$row =& JTable::getInstance('items', 'Table');
		$row->load( $uid );
		$row->move( $inc, 'catid = '. (int) $row->country .' AND published != 0' );
		
		$msg="MOVED! ".$row->country;
		$mainframe->redirect( 'index.php?option=com_bbitsd&task=items',$msg );
	
	}    

    
}
?>